﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
using Liang.Model;
using System.Text.RegularExpressions;

namespace Liang.Util.ProtocolDataParser
{
    /// <summary>
    /// 协议数据解析类
    /// </summary>
    public class ProtocolData : IProtocolData
    {
        /// <summary>
        /// 正则表达式
        /// </summary>
        private Regex regex = null;

        /// <summary>
        /// 定长协议数据
        /// </summary>
        /// <param name="format"></param>
        /// <param name="nHeadSize"></param>
        /// <param name="nEndSize"></param>
        public ProtocolData(byte[] format, uint nHeadSize, uint nEndSize)
        {
            if (format.Length > (nHeadSize + nEndSize))
            {
                byte[] FrameHead = new byte[nHeadSize];
                Array.Copy(format, FrameHead, nHeadSize);
                string strFrameHead = Bytes.ByteArrayToHexString(FrameHead);

                byte[] FrameEnd = new byte[nEndSize];
                Array.Copy(format, (format.Length - nEndSize), FrameEnd, 0, nEndSize);
                string strFrameEnd = Bytes.ByteArrayToHexString(FrameEnd);

                string pattern = string.Format(@"{0}(.+){1}", strFrameHead, strFrameEnd);
                regex = new Regex(pattern, RegexOptions.IgnoreCase);
            }
            else
            {
                throw new Exception("无效的数据格式");
            }
        }

        /// <summary>
        /// 不定长协议数据
        /// </summary>
        /// <param name="frameHead"></param>
        /// <param name="frameEnd"></param>
        public ProtocolData(byte[] frameHead, byte[] frameEnd)
        {
            string strFrameHead = string.Empty;
            string strFrameEnd = string.Empty;

            if (null != frameHead && null != frameEnd)
            {
                // 有头有尾
                strFrameHead = Bytes.ByteArrayToHexString(frameHead);
                strFrameEnd = Bytes.ByteArrayToHexString(frameEnd);
            }
            else if (null != frameHead && null == frameEnd)
            {
                // 有头无尾
                strFrameHead = Bytes.ByteArrayToHexString(frameHead);
                strFrameEnd = Bytes.ByteArrayToHexString(frameHead);
            }
            else if (null == frameHead && null != frameEnd)
            {
                // 有尾无头
                strFrameHead = Bytes.ByteArrayToHexString(frameEnd);
                strFrameEnd = Bytes.ByteArrayToHexString(frameEnd);
            }
            else
            {
                throw new Exception("无效的数据格式");
            }

            string pattern = string.Format(@"{0}(.+){1}", strFrameHead, strFrameEnd);
            regex = new Regex(pattern, RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 解析数据
        /// </summary>
        /// <param name="inBuffer"></param>
        /// <param name="outBuffer"></param>
        /// <returns></returns>
        public virtual int ParsingData(byte[] inBuffer, out byte[] outBuffer)
        {
            // 已处理的数据长度
            int nProcessedDataLength = 0;
            outBuffer = null;

            try
            {
                // 将输入的数据转换为16进制字符串
                string inHexString = Bytes.ByteArrayToHexString(inBuffer);

                // 匹配搜索
                Match match = regex.Match(inHexString);

                if (match.Success)
                {
                    int nHeadIndex = match.Index / 2;
                    int nDataLength = match.Value.Length / 2;
                    outBuffer = new byte[nDataLength];
                    Array.Copy(inBuffer, nHeadIndex, outBuffer, 0, nDataLength);
                    nProcessedDataLength = (match.Index + match.Value.Length) / 2;
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(typeof(ProtocolData), e);
            }

            return nProcessedDataLength;
        }
    }
}
